# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 波士顿房价预测（3过拟合）.py
# @Author: dongguangwen
# @Date  : 2025-02-06 13:58
# 0.导入工具包
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

# 1.准备数据
np.random.seed(22)
x = np.random.uniform(-3, 3, size=100)
# print(x)
y = 0.5 * x ** 2 + np.random.normal(0, 1, size=100)
print(y)

# 2.模型训练
model = LinearRegression()  # 实例化线性回归模型
X = x.reshape(-1, 1)  # 线性回归模型需要二维数组
X2 = np.hstack([X, X ** 2, X ** 3, X ** 4, X ** 5, X ** 6, X ** 7, X ** 8, X ** 9, X ** 10])  # 数据增加多次项
model.fit(X2, y)

# 3.模型预测
y_pred = model.predict(X2)
print(y_pred)
print('权重参数值：', model.coef_)

# 4.计算均方误差
myret = mean_squared_error(y, y_pred)
print('myret-->', myret)

# 5.展示效果
plt.scatter(X, y)
# 画图plot折线图时 需要对x进行排序, 取x排序后对应的y值
plt.plot(np.sort(x), y_pred[np.argsort(x)], color='red')
plt.show()

"""
[ 2.68991269 -0.15896455 -1.0657469   1.77691179  2.2169277   1.44760438
  1.95468165  1.43909861  0.15853376  1.32780734  4.86511909  0.39702949
  2.63475246 -0.14604323  2.36460552  1.62061024  0.83996403  3.84213349
  0.38401779  1.60688722  1.07599777  0.65478898  0.9188096  -0.64424027
  1.59171645 -0.51865919  1.34359201  2.92620657  2.80123304  4.40659537
  0.87811453  1.10113724  6.1923925   0.855368    2.53145803  2.20814387
  0.50914933  3.56598319 -1.52802892 -0.22965108  0.68003709  4.97404497
 -0.75143267  2.88409744  5.60301458  0.99887944  2.62802974  2.46495793
  3.66571442 -1.85804906  0.83140672  0.86165142  0.33581663  0.06046012
  2.05133222  0.89686498  0.570795    4.81741428 -0.24435866  1.68542272
  2.49369326  2.83636823 -0.23086228  2.26822132  2.33381088  0.67749307
  1.59425991  2.65328755  3.70118035  3.9285876   1.95288399  2.60958529
  3.87293405  3.76537917  0.40451931  3.28686949  0.78030988  4.27240723
  2.19597273 -0.5814483   1.44486224  0.43852349 -0.27671756  2.04600828
  4.32094833 -0.20276307  2.15265487  1.69291982  0.10829686  5.84398733
  0.48910081  5.08461625  0.35116797  2.44363252  0.07344753  1.08868863
  1.41343863  2.96338708  4.35563861  1.45501813]
[ 1.88035172 -0.24950355  0.04346862  2.58027545  2.11766972  1.06011508
  1.68748473  0.85477987  1.84593442  1.80876193  3.8755999   0.14791772
  1.8368454   1.0628619   1.96964841  3.45064387  1.28297048  2.99077802
  0.88367207  1.50484977  1.242906    0.84749184  0.42127365  0.55139412
 -0.01972396  0.33079631  0.88560809  3.3275525   3.02685447  3.88941203
  0.81902143  1.56044584  4.91490052  2.05404691  2.79310302  1.06181025
 -0.19290223  2.8710724  -0.24852606 -0.15880877  1.37751611  5.09550702
 -0.09843311  3.83655395  5.09723416  0.83871419  2.42429145  2.95127805
  2.85282655 -0.24500474  1.92377382  1.38796189  0.5004355  -0.13657445
  3.51428112  0.90393642  1.85022922  2.15836629 -0.02067179  1.35543516
  3.0332862   4.15409712  1.34864232  1.84917372  1.0144216   2.08005481
  1.05952463  2.05600081  2.56933366  3.38305655  0.81391981  2.97315111
  2.9780653   4.69421528  0.80678264  2.19929939  0.20292853  1.7930531
  2.2847403  -0.20956878  0.23212447 -0.2404147  -0.19259577  1.24269772
  2.9124613   1.76452754  2.62307838  0.66390198  0.87092217  4.95204744
  0.75708214  4.77881107 -0.19888565  3.00325747 -0.11955911  1.9125732
  1.68475578  1.74234583  4.94510835  1.49542995]
权重参数值： [ 4.83901463e-01  1.91708406e+00 -1.09189930e+00 -9.60331272e-01
  5.22612600e-01  2.48127460e-01 -9.01884785e-02 -2.65810071e-02
  5.04735223e-03  9.83074618e-04]
myret--> 0.8144736129032124
"""
